<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/9/20
 * Time: 10:47
 */

namespace app\api\model;

use think\paginator;

class Order extends BaseModel
{
    protected  $hidden=['user_id','delete_time','create_time'];

    protected $autoWriteTimestamp = true;

    /**
     * 读取器，改变模型里面部分属性
     * @param $value
     * @return null|string
     */
    public function getSnapItemsAttr($value){
        if (empty($value)){
            return null;
        }
        return json_decode($value);
    }
    /**
     * 读取器
     * @param $value
     * @return null|string
     */
    public function getSnapAddressAttr($value){
        if (empty($value)){
            return null;
        }
        return json_decode($value);
    }
    /**
     * 订单入库
     * @param $orderSn
     * @param $uid
     * @param $snap
     * @return $this
     */
    public static  function createOrder($orderSn,$uid,$t_id,$wu_id,$snap)
    {
        $orderInfo = [
            'order_no'=>$orderSn,
            'user_id'=>$uid,
            't_id'=>$t_id,
            'wu_id'=>$wu_id,
            'total_price'=> $snap['orderPrice'],
            'snap_img'=>$snap['snapImg'],
            'snap_name'=>$snap['snapName'],
            'total_count'=>$snap['totalCount'],
            'snap_items'=>json_encode($snap['pStatusArray']),
            'snap_address'=>$snap['snapAddress']
        ];
        $order  = self::create($orderInfo);
        return $order;
    }
    /**
     *根据orderId获取订单信息
     * @param $orderId
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getOrderInfoByID($orderId){
        $orderInfo = self::where('id','=',$orderId)->find();
        return $orderInfo;
    }
    /**
     * 根据orderNo获取订单信息
     * @param $OrderNo
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getOrderInfoByOrderNo($OrderNo){
        $orderInfo = self::where('order_no','=',$OrderNo)->lock(true)->find();
        return $orderInfo;
    }
    /**
     * 更新订单表
     * @param $orderID
     * @param $info
     * @return $this
     */
    public static function updateOrderByID($orderID,$info)
    {
        return self::where('id','=',$orderID)->update($info);
    }
    /**
     * 根据用户id获取订单表
     * @param $uid
     * @param $page
     * @param $size
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getOrderByUserID($uid,$status,$page,$size){
        $condition = ' 1=1';
        if ($status != 100){//全部订单
            $condition .= " and status = $status ";
        }
        $result =  self::where('user_id','=',$uid)
                ->where($condition)
                ->order('create_time desc')
                ->paginate($size,true,['page'=>$page]);
        return $result;
    }
    /**
     * 获取摸个管理员的所有订单
     * @param $page
     * @param $size
     * @return Paginator
     */
    public static function getOrderByManager($status,$t_id,$page,$size){
        $condition = ' 1=1';
        if ($status != 100){//全部订单
            $condition .= " and status = $status ";
        }
        $result = self::where('t_id','=',$t_id)
            ->where($condition)
            ->order('create_time desc')
            ->paginate($size,false,['page'=>$page]);
        return $result;
    }
    /**
     * 获取用户全部订单列表
     * @param $uid
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getOrderList($uid){
        $result =  self::where('user_id','=',$uid)
            ->order('create_time desc')
           ->select();
        return $result;
    }

    /**
     *获取用户分类订单
     * @param $uid
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getOrderByClass($uid,$status,$page,$size){
        $result = self::where('status','=',$status)
                ->where('user_id','=',$uid)
                ->order('create_time desc')
                ->paginate($size,true,['page'=>$page]);
        return $result;
    }

    /**
     * 获取总订单数
     * @param $t_id
     * @return int|string
     */
    public static function getAllOrders($t_id,$time){
        $result = self::where('status = 2 or status = 4  ')
            ->where('t_id','=',$t_id)
            ->where('update_time','>',$time)
            ->count();
        return $result;
    }

    /**
     * 获取订单总价
     * @return float|int
     */
    public static function getAllPrice($t_id,$time){
        $result =  self::where('status = 2 or status = 4  ')
            ->where('t_id','=',$t_id)
            ->where('update_time','>',$time)
            ->sum('total_price');
        return $result;
    }

    /**
     * 订单，金额统计
     * @param $t_id
     * @param $time
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getOrderByTime($t_id,$time){
        $result = self::where('t_id','=',$t_id)
            ->where('status','in',[2,3,4])
            ->where('create_time','>',$time)
            ->field("FROM_UNIXTIME(update_time,'%m-%d') days,count(id) num,sum(total_price) price")
            ->group('days')
            ->select();
        return $result;
    }

    /**
     * 查询商品信息
     * @param $condition
     * @param $t_id
     * @param $page
     * @param $size
     * @return Paginator
     */
    public static function searchOrder($condition,$t_id,$page,$size){
        $result = self::where('t_id','=',$t_id)
                ->where('snap_name','like','%'.$condition.'%')
                ->whereOr('order_no','like','%'.$condition.'%')
                ->order('create_time desc')
                ->paginate($size,false,['page'=>$page]);
        return $result;
    }

}